package data_structure;

public class leetcode_0142 {
    public ListNode detectCycle(ListNode head) {
        if(head==null) return null;
        ListNode fast=head;
        ListNode slow=head;
        do{
            if(fast.next==null||fast.next.next==null) return null;
            fast=fast.next.next;
            slow=slow.next;
        }while (fast!=slow);
        slow=head;
        while(slow!=fast){
            fast=fast.next;
            slow=slow.next;
        }
        return fast;
    }
}
